<template>
<div class="article-comments">
    <!-- 导航栏 -->
    <van-nav-bar :title="comment.reply_count+'条回复'">
      <van-icon slot="left" name="cross" @click="$emit('close')"/>
    </van-nav-bar>
    <!-- /导航栏 -->

    <!-- 当前评论 -->
    <van-cell title="当前评论">
      <van-image
        slot="icon"
        round
        width="30"
        height="30"
        style="margin-right: 10px;"
        :src="comment.aut_photo"
      />
      <span style="color: #466b9d;" slot="title">{{comment.aut_name}}</span>
      <div slot="label">
        <p style="color: #363636;">{{comment.content}}</p>
        <p>
          <span style="margin-right: 10px;">{{comment.pubdate|relativeTime}}</span>
          <van-button
            size="mini"
            type="default"
          >回复 {{comment.reply_count}}</van-button>
        </p>
      </div>
      <van-icon slot="right-icon" />
    </van-cell>
    <!-- /当前评论 -->
    <van-divider>全部回复</van-divider>

    <!-- 对当前评论 回复列表 -->
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
     >
      <van-cell
        v-for="(item, index) in list"
        :key="index"
        :title="item.aut_name"
      >
        <van-image
          slot="icon"
          round
          width="30"
          height="30"
          style="margin-right: 10px;"
          :src="item.aut_photo"
        />
        <span style="color: #466b9d;" slot="title">{{item.aut_name}}</span>
        <div slot="label">
          <p style="color: #363636;">{{item.content}}</p>
          <p>
            <span style="margin-right: 10px;">{{item.pubdate|relativeTime}}</span>
          </p>
        </div>
      </van-cell>
    </van-list>
    <!-- 回复列表 -->

    <!-- 发布回复 -->
    <van-cell-group class="publish-wrap">
      <van-field
        clearable
        placeholder="请输入回复内容"
        v-model.trim="commentText"
      >
        <van-button
          slot="button"
          size="mini"
          type="info"
          @click="hAddReply"
        >发布</van-button>
      </van-field>
    </van-cell-group>
    <!-- /发布回复 -->
</div>

</template>
<script>
import { getComments, addComment } from '@/api/comment'
export default {
  name: 'CommentReply',
  data () {
    return {
      list: [], // 评论列表
      offset: null,
      commentText: '', // 回复的内容
      loading: false, // 上拉加载更多的 loading
      finished: false // 是否加载结束
    }
  },
  props: {
    comment: {
      type: Object,
      required: true
    }
  },
  methods: {
    async onLoad () {
      // 发请求 拿数据
      const result = await getComments({
        type: 'c',
        source: this.comment.com_id.toString(),
        offset: this.offset // 获取评论数据的偏移量
      })
      console.log(result)

      // 将数据添加到列表
      this.list.push(...result.data.data.results)
      // 加载状态结束
      this.loading = false
      // 数据全部加载完成
      if (result.data.data.results.length) {
        // 发下一次请求时，要把上一次请求回来的最后一个id传入
        this.offset = result.data.data.last_id
      } else {
        this.finished = true
      }
    },
    // 点击发布
    async hAddReply () {
      // 添加回复
      // 1 拿到用户输入的内容
      if (!this.commentText) {
        return
      }
      // 2 调接口
      const result = await addComment({
        target: this.comment.com_id.toString(), // 评论id
        content: this.commentText, // 回复内容
        art_id: this.$route.params.id// 文章id
      })

      // 3 根据调用的结果给反馈
      this.list.unshift(result.data.data.new_obj)
      this.comment.reply_count++
      this.$toast('回复成功')
      // 4 清空输入的内容
      this.commentText = ''
    }
  }
}
</script>
<style lang="less" scoped>
// 发表评论的区域是固定在下端的
.publish-wrap {
  position: fixed;
  left: 0;
  bottom: 0;
  width: 100%;
}
// 给发表评论区空出地方
.van-list {
  margin-bottom: 45px;
}
</style>
